/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.mail.manage.service.processor.active;

import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.ServiceOrderBase;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.mail.domain.MailTemplate;
import info.peigen.hotpot.component.mail.manage.service.order.MailTemplateEditOrder;
import info.peigen.hotpot.component.mail.manage.service.processor.AbstractMailTemplateServiceProcessor;
import org.noear.solon.annotation.Component;

/**
 * <b>(MailTemplateActiveProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/22
 */
@SuppressWarnings("rawtypes")
@Component
public class MailTemplateActiveProcessor extends AbstractMailTemplateServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 10;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        String       templateId   = switchId(serviceContext);
        MailTemplate mailTemplate = mailTemplateRepository.active(templateId);
        Assert.notNull(mailTemplate, () -> new BusinessException(ResultCode.FAILURE, "未能激活mailTemplate[templateId=" + templateId + "]"));
        mailTemplate.check();
        serviceContext.setDomain(mailTemplate);
    }

    private String switchId(ProcessorServiceContext serviceContext) {
        ServiceOrderBase order = serviceContext.getOrder();
        if (order instanceof MailTemplateEditOrder) {
            return ((MailTemplateEditOrder) order).getTemplateId();
        } else {
            return null;
        }
    }
}